﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMSmanagement.IBaseService;
using WMSmanagement.IBaseService.OuStorage;
using WMSmanagement.IBaseService.OuStorage.Dtos;
using WMSmanagement.Model;

namespace WMSmanagement.BaseService
{
    [ApiExplorerSettings(GroupName = "焦仁豪")]
    public class OuStorageService : ApplicationService, IOuStorageService
    {
        private readonly IRepository<OuStorage, int> _ouStorageRepository;
        private readonly IRepository<OuStorageDetails, int> _ouStorageDetailsRepository;
        private readonly IRepository<Products, int> _productsRepository;

        public OuStorageService(IRepository<OuStorage, int> ouStorageRepository, IRepository<OuStorageDetails, int> ouStorageDetailsRepository, IRepository<Products, int> productsRepository)
        {
            _ouStorageRepository = ouStorageRepository;
            _ouStorageDetailsRepository = ouStorageDetailsRepository;
            _productsRepository = productsRepository;
        }
        /// <summary>
        /// 获取所有出库信息
        /// </summary>
        /// <returns></returns>
        public async Task<OuStoragePageDtos> GetOuStorages(string? StockOutCode, string? StockOutType, int pageIndex, int pageSize)
        {
            var ouStorages = from a in await _ouStorageDetailsRepository.GetListAsync()
                             join b in await _ouStorageRepository.GetListAsync() on a.StockOutCode equals b.StockOutCode
                             join c in await _productsRepository.GetListAsync() on a.ProductCode equals c.ProductCode
                             select new OuStorageDtos
                             {
                                 StockOutCode = b.StockOutCode,
                                 StockInWarehouse = b.StockInWarehouse,
                                 StockInLocation = b.StockInLocation,
                                 StockOutType = b.StockOutType,
                                 PurchaseOrderCode = b.PurchaseOrderCode,
                                 CustomerCode = b.CustomerCode,
                                 CustomerName = b.CustomerName,
                                 CustomerContact = b.CustomerContact,
                                 Maker = b.Maker,
                                 CustomerPhone = b.CustomerPhone,
                                 MakeTime = b.MakeTime,
                                 Remark = b.Remark,
                                 ProductCode = c.ProductCode,
                                 ProductName = c.ProductName,
                                 ProductSpecification = c.ProductSpecification,
                                 Category = c.Category,
                                 StorageUnit = c.StorageUnit,
                                 Unit = c.Unit,
                                 UnitPrice = c.UnitPrice,
                                 Quantity = a.Quantity,
                             };

            ouStorages = ouStorages.WhereIf(!string.IsNullOrEmpty(StockOutCode), a => a.StockOutCode.Contains(StockOutCode));
            ouStorages = ouStorages.WhereIf(!string.IsNullOrEmpty(StockOutType), a => a.StockOutType == StockOutType);
            List<OuStorageDtos>? result = ouStorages.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var list = ouStorages.Count();
            return new OuStoragePageDtos() { TotalCount = list, Data = result };
        }
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductsDTO> GetProducts(int id)
        {
            var products = await _productsRepository.GetAsync(id);
            return new ProductsDTO()
            {
                ProductCode = products.ProductCode,
                ProductName = products.ProductName,
                ProductSpecification = products.ProductSpecification,
                Category = products.Category,
                StorageUnit = products.StorageUnit,
                Unit = products.Unit,
                UnitPrice = products.UnitPrice,
            };
        }
        /// <summary>
        /// 删除出库信息
        /// </summary>
        /// <param name="StockOutCode"></param>
        /// <returns></returns>
        public async Task<bool> DeleteOuStorage(string StockOutCode)
        {
            await _ouStorageRepository.DeleteAsync(a => a.StockOutCode == StockOutCode);
            var ouStorageDetails = await _ouStorageDetailsRepository.GetListAsync(a => a.StockOutCode == StockOutCode);
            await _ouStorageDetailsRepository.DeleteManyAsync(ouStorageDetails);
            return true;
        }
    }
}
